package copytranslation.write;

import copytranslation.hash.Hash;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;

/**
 *
 * @author srkrishnan
 */
public class writeExcel {

    private String desexcelPath;
    private HashMap translationmap;
    private int KeyColumnindex;
    private int valueColumnindex;
    final static private int[] SHEETS = {0, 1};

    public writeExcel() {
    }

    public writeExcel(String desexcelPath) {
        this.desexcelPath = desexcelPath;
    }

    public int getKeyColumnindex() {
        return KeyColumnindex;
    }

    public void setKeyColumnindex(int KeyRowindex) {
        this.KeyColumnindex = KeyRowindex;
    }

    public int getValueColumnindex() {
        return valueColumnindex;
    }

    public void setValueColumnindex(int valueRowindex) {
        this.valueColumnindex = valueRowindex;
    }

    public String getDesexcelPath() {
        return desexcelPath;
    }

    public void setDesexcelPath(String desexcelPath) {
        this.desexcelPath = desexcelPath;
    }

    public void addTranslationLabelstoExcel() {
        translationmap = Hash.getTranslationmap();
        System.out.println("Total unique Labels :" + translationmap.size());
        processFolder(desexcelPath);
    }

    /*
     * process the given folder recursively
     */
    private void processFolder(String filepath) {
        File file = new File(filepath);
        if (!file.exists()) {
            return;
        }
        //If not directory
        if (!file.isDirectory()) {
            //System.out.println("Processing.... " + file.getName());
            if (file.getName().endsWith(".xls") || file.getName().endsWith(".XLS")) {
                addTranslation(file);
            }
            return;
        }
        String[] filelist = file.list();
        for (String filename : filelist) {
            processFolder(filepath + File.separator + filename);
        }
    }

    private void addTranslation(File file) {
        InputStream input = null;
        POIFSFileSystem fs;
        HSSFWorkbook wb;
        HSSFSheet sheet;
        try {
            input = new FileInputStream(file);
            fs = new POIFSFileSystem(input);
            wb = new HSSFWorkbook(fs);
            for (int sheetno : SHEETS) {
                sheet = wb.getSheetAt(sheetno);

                String hashKey = "";
                int Keyindex = this.getKeyColumnindex();
                int valueindex = this.getValueColumnindex();

                // Iterate over each row in the sheet
                Iterator rows = sheet.rowIterator();
                while (rows.hasNext()) {
                    HSSFRow row = (HSSFRow) rows.next();

                    //get KEY and VALUE from row and put it in HashMap
                    if (row.getCell(Keyindex).getCellType() == Cell.CELL_TYPE_STRING) {
                        hashKey = row.getCell(Keyindex).getStringCellValue();
                        if (translationmap.containsKey(hashKey)) {
                            Cell cell = row.getCell(valueindex);
                            if (cell == null) {
                                cell = row.createCell(valueindex);
                            }
                           
                                row.getCell(valueindex).setCellValue("" + translationmap.get(hashKey));
                            
                        }
                    }
                }
            }
            //System.out.println("" + translationmap.size());
            input.close();
            FileOutputStream fOut = new FileOutputStream(file);
            // Write the Excel sheet
            wb.write(fOut);
            fOut.flush();
            fOut.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                input.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
